package com.shixj.easy.spider.factory;

import com.shixj.easy.spider.Util;
import com.shixj.easy.spider.model.Chapter;
import com.shixj.easy.spider.model.RegexConfig;
import com.shixj.ef.utils.FetchHelper;
import com.shixj.ef.vo.FetchResult;
import org.jsoup.nodes.Document;

/**
 * Created by shixj on 2018/1/15.
 */
public class DefaultChapterFactory implements EntityFactory {

    /**
     * 转换novel封面信息
     *
     * @param result
     * @return
     */
    @Override
    public Chapter build(FetchResult result,RegexConfig regexConfig) {
        Document document = result.getDocument();
        Chapter chapter = new Chapter();
        chapter.setTitle(getPartText(document, regexConfig.getTitleRegex()));
        //sourceID
        chapter.setSourceId(FetchHelper.subByRegex(result.getUrl(), regexConfig.getChapterSourceIdRegex(),0));
        //content
        chapter.setContent(getPartHtml(document, regexConfig.getContentRegex()));
        chapter.setOrderNo(Util.tranWordsToNum(chapter.getTitle()));
        return chapter;
    }
}
